package com.example.android_web_4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.utils.CallBack;
import com.example.utils.HttpUtils;

public class UserRegisterActivity extends Activity {

    private EditText uname,upwd,urealname;
    String basePath="http://"+getResources().getString(R.string.ipaddress)+":"+getResources().getString(R.string.serverport)+"/" +
            getResources().getString(R.string.project);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_register);
        uname=(EditText)findViewById(R.id.uname);
        upwd=(EditText)findViewById(R.id.upwd);
        urealname=(EditText)findViewById(R.id.urealname);
    }

    //执行注册
    String message="";
    public void doRegister(View view){
        final String name=uname.getText().toString();
        String pwd=upwd.getText().toString();
        String realname=urealname.getText().toString();
        String url=basePath+"/TbUserServlet";
        String data="method=register&uname="+name+"&upwd="+pwd+"&urealname="+realname;
        HttpUtils.doPost(url, data, new CallBack() {
            @Override
            public void success(String str) {
                if("true".equals(str)){
                    message="添加用户成功!";
                    Intent intent=new Intent(UserRegisterActivity.this,MainActivity.class);
                    intent.putExtra("userName",name);
                    setResult(RESULT_OK,intent);
                    finish();
                }else{
                    message="添加用户失败!";
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(UserRegisterActivity.this, message, Toast.LENGTH_SHORT).show();
                    }
                });
            }
            @Override
            public void failed(String str) {
            }
        });
    }

    //返回
    public void doBack(View view){
        this.finish();
    }
}
